/// <reference path="../../Resources/js/jquery/jquery.debug.js" />
/// <reference name="MicrosoftAjax.debug.js" />


function LeafItem(node, basis) {
    LeafItem.initializeBase(this, [node, basis]);
    // this.locale = LeafItem.getCurrentLocale();
    this.dataType = node.getAttribute('dataType');
    this.addChanged(Function.createDelegate(this, this.validate));
    this.addLoading(Function.createDelegate(this.getValueHandler(), this.getValueHandler().load));
    this.addUnloading(Function.createDelegate(this.getValueHandler(), this.getValueHandler().unload));
}

LeafItem.Label = "Lb";

LeafItem.prototype = {
    getValueHandlerType: function() {
        /// <returns type="String" >
        /// the value handler type used for the valuehandler factory
        /// </returns>
        if (this.vhType == null) {
            this.vhType = this.node.getAttribute("type");
        }

        return this.vhType;
    },

    getValueHandler: function() {
        /// <returns type="BM.ValueHandler" >
        /// the value handler of the item
        /// </returns>
        if (this.valueHandler == null) {
            this.valueHandler = new BM.ValueHandler[this.getValueHandlerType()](this);
        }
        return this.valueHandler;
    },

    getInput: function() {
        /// <returns domElement="true" >
        /// the input (when more, the first input) of the Leaf item
        /// </returns>
        return this.getValueHandler().getInput();
    },

    getInputs: function() {
        /// <returns type="jQuery">
        /// the inputs array of the Leaf item
        /// </returns>
        return this.getValueHandler().getInputs();
    },

    getLocale: function() {
        /// <returns type="String" >
        /// the locale of the leaf
        /// </returns>        
        return this.locale;
    },

    getLabels: function() {
        /// <returns type="jQuery" >
        /// all the labels of the input (all the nosed with Lb class
        /// </returns>
        return $(this.node).children("." + LeafItem.Label);
    },

    /*fixLocaleValue: function(value, locale) {
    if (!BM.DataTypeConstraint.isNumberDataType(this.dataType)) return value;
    if (this.getLocale() != locale && (locale == 'pl' || this.getLocale() == 'pl')) {
    value = ValueParser.reverseDotComma(value);
    }
    return value;
    },*/

    setValue: function(value, locale) {
        this.getValueHandler().setValue(value);
    },

    getValue: function() {
        return this.getValueHandler().getValue();
    },

    getDataTypeValue: function() {
        if (BM.DataTypeConstraint.isDateDataType(this.dataType)) {
            var format = this.getMForm().get_culturalContext().getFormat(this.dataType);
            var date = Date.parseLocale(this.getValue(), format);
            return date;
        }

        if (BM.DataTypeConstraint.isNumberDataType(this.dataType)) {
            var number = Number.parseLocale(this.getValue());
            return number;
        }

        return this.getValue();
    },

    setRequired: function() {
        LeafItem.callBaseMethod(this, 'setRequired');
        this.getLabels().removeClass("OpLb").addClass("RqLb");
        this.getInputs().removeClass("OpIn").addClass("RqIn");
    },

    setOptional: function() {
        LeafItem.callBaseMethod(this, 'setOptional');
        this.getLabels().removeClass("RqLb").addClass("OpLb");
        this.getInputs().removeClass("RqIn").addClass("OpIn");
    },

    focus: function() {
        if (this.getNode().style.display == 'none') return false;
        this.raiseFocusing();
        var input = this.getInput();
        if ($(input).is("*:visible[disabled != 'disabled']")) {
            return MForm.focus(input);
        }
        return false;
    },

    recountAdditions: function() {
        LeafItem.callBaseMethod(this, 'recountAdditions');
        if (this.getMForm().get_shouldValidateInViewMode() || this.getMode() == MItem.EditMode) {
            this.constraints[this.constraints.length] = new BM.DataTypeConstraint(this);
        }

    },

    validate: function() {
        if (this.isOptional() && this.getValue() == '') {
            this.raiseOnValidationSuccess([]);
            return true;
        }

        return this.validateSelf();
    },

    isDisabled: function() {
        return !this.getInputs().is(':enabled');
    },

    setDisabled: function() {
        this.getInputs().each(function() {
            this.disabled = true;
        });

    },

    setEnabled: function() {
        this.getInputs().each(function() {
            this.disabled = false;
        });
    }

}

/*LeafItem.getCurrentLocale = function() {
return Sys.CultureInfo.CurrentCulture.name.substring(0, 2);
}*/

LeafItem.registerClass("LeafItem", MItem);
